/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.Ventas;

import Atxy2k.CustomTextField.RestrictedTextField;
import javax.swing.table.AbstractTableModel;
import java.util.Date;
import java.util.ArrayList;
import Controlador.Service.Ventas.GestionVentas;
import Controlador.Bean.Ventas.FacturacionBusqueda;
import javax.swing.JOptionPane;
import javax.swing.table.TableRowSorter;
import lib.Validaciones;

/**
 *
 * @author Luis Enrique
 */
public class FacturacionMainPanel extends javax.swing.JPanel {

    PedidoTableModel myTableModel;
    Date fecha = new Date ();
    ArrayList<FacturacionBusqueda> documentos = GestionVentas.getInstance().queryAllDocumentos();
        
    class PedidoTableModel extends AbstractTableModel{
        String [] titles = {"Núm. de Documento","Tipo de Doc.","Doc. de Cliente","Razón Social/Nombre","Monto Total"};

        @Override
        public int getRowCount() {
            return documentos.size();
        }

        @Override
        public int getColumnCount() {
            return titles.length;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            String res=null;
            FacturacionBusqueda fb = documentos.get(rowIndex);
            switch(columnIndex){
                case 0: res = fb.getIdFactura(); break;
                case 1: res = fb.getTipo();break;
                case 2: res = fb.getNumDocCliente(); break;
                case 3: res = (fb.getRazonSocial()!=null)?fb.getRazonSocial():(fb.getApePaterno()+" "+fb.getApeMaterno()+", "+fb.getNombres());break;
                case 4: res = fb.getTotalSoles()+""; break;
            }
            return res;
        }
        
        @Override
        public String getColumnName(int col){
            return titles[col];
        }
    }
    
    public FacturacionMainPanel() {
        initComponents();
        btnGpMoneda.add(rbDolares);
        btnGpMoneda.add(rbSoles);
        btnGpTipo.add(rbBoleta);
        btnGpTipo.add(rbFactura);
                
        RestrictedTextField restricted = new RestrictedTextField(txtNombre);
        restricted.setLimit(50);
        
        restricted = new RestrictedTextField(txtNumDoc);
        restricted.setLimit(11);
        restricted.setOnlyNums(true);
        
        restricted = new RestrictedTextField(txtNumPedido);
        restricted.setLimit(6);
        restricted.setOnlyNums(true);
        
        restricted = new RestrictedTextField(txtNumCotizacion);
        restricted.setLimit(6);
        restricted.setOnlyNums(true);
        
        myTableModel = new PedidoTableModel();
        tblDocs.setModel(myTableModel);
        TableRowSorter<PedidoTableModel> sorter = new TableRowSorter<> ((PedidoTableModel) tblDocs.getModel());
        tblDocs.setRowSorter(sorter);
    }
    
    public void refreshItemsTable(){
        this.documentos = GestionVentas.getInstance().queryAllDocumentos();
        myTableModel.fireTableChanged(null);        
    }
    
    public void refreshItemsTable2(ArrayList<FacturacionBusqueda> docsBusq){
        this.documentos = new ArrayList(docsBusq);
        myTableModel.fireTableChanged(null);        
    }
                
    public void refreshDetails(){
        if(rbSoles.isSelected()){
            txtFinalSoles.setText("");
            txtFinalSoles.setEnabled(true);
            txtFinalDolares.setText("");
            txtFinalDolares.setEnabled(false);
        }
        else {
            txtFinalSoles.setText("");
            txtFinalSoles.setEnabled(false);
            txtFinalDolares.setText("");
            txtFinalDolares.setEnabled(true);        
        }          
    }
    
    public boolean valido(){
        boolean b=true;
        if(!txtFinalSoles.getText().isEmpty())
            if(!Validaciones.validarReal(txtFinalSoles.getText())){
                 b = false;
                JOptionPane.showMessageDialog(this, "Error en Monto Final Soles.", "Error en los datos", JOptionPane.OK_OPTION);
            }
        if(!txtFinalDolares.getText().isEmpty())
            if(!Validaciones.validarReal(txtFinalDolares.getText())){
                 b = false;
                JOptionPane.showMessageDialog(this, "Error en Monto Final Dolares.", "Error en los datos", JOptionPane.OK_OPTION);
            }        
        return b;
    }
    
    public void limpiar(){
        txtNumPedido.setText("");
        dpFechaPed.setDate(null);
        txtNumCotizacion.setText("");
        dpFechaCot.setDate(null);
        txtNombre.setText("");
        txtNumDoc.setText("");
        dpFechaVenta.setDate(null);
        btnGpTipo.clearSelection();
        btnGpMoneda.clearSelection();
        txtFinalSoles.setText("");
        txtFinalDolares.setText("");
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnGpMoneda = new javax.swing.ButtonGroup();
        btnGpTipo = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblDocs = new javax.swing.JTable();
        jPanel4 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtNumDoc = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        txtNumPedido = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtNumCotizacion = new javax.swing.JTextField();
        dpFechaCot = new org.jdesktop.swingx.JXDatePicker();
        dpFechaPed = new org.jdesktop.swingx.JXDatePicker();
        jLabel4 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        dpFechaVenta = new org.jdesktop.swingx.JXDatePicker();
        jPanel2 = new javax.swing.JPanel();
        rbSoles = new javax.swing.JRadioButton();
        rbDolares = new javax.swing.JRadioButton();
        jLabel8 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        txtFinalSoles = new javax.swing.JTextField();
        txtFinalDolares = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        rbBoleta = new javax.swing.JRadioButton();
        rbFactura = new javax.swing.JRadioButton();
        btnBuscar = new javax.swing.JButton();
        btnLimpiar = new javax.swing.JButton();
        btnRegistrar = new javax.swing.JButton();

        setMaximumSize(new java.awt.Dimension(784, 549));
        setMinimumSize(new java.awt.Dimension(784, 549));
        setPreferredSize(new java.awt.Dimension(784, 549));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Lista de Documentos de Ventas"));
        jPanel1.setToolTipText("");
        jPanel1.setMaximumSize(new java.awt.Dimension(764, 305));
        jPanel1.setMinimumSize(new java.awt.Dimension(0, 0));
        jPanel1.setPreferredSize(new java.awt.Dimension(764, 305));
        jPanel1.setLayout(new java.awt.BorderLayout());

        tblDocs.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblDocs.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblDocsMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tblDocs);

        jPanel1.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Criterios de Búsqueda"));

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Información de la Venta"));

        jLabel1.setText("Razón Social/Nombre");

        jLabel2.setText("Número de Documento");

        jLabel10.setText("Número de Pedido");

        jLabel5.setText("Número de la Cotización");

        jLabel4.setText("Fecha del Pedido");

        jLabel7.setText("Fecha de la Cotización");

        jLabel3.setText("Fecha de Emisión");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtNumDoc, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txtNombre)
                            .addComponent(dpFechaVenta, javax.swing.GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel10)
                            .addComponent(jLabel4)
                            .addComponent(jLabel7)
                            .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(dpFechaCot, javax.swing.GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE)
                            .addComponent(txtNumCotizacion)
                            .addComponent(txtNumPedido)
                            .addComponent(dpFechaPed, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNumPedido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dpFechaPed, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNumCotizacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dpFechaCot, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNumDoc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dpFechaVenta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Información del Pago"));

        rbSoles.setText("Soles");
        rbSoles.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbSolesActionPerformed(evt);
            }
        });

        rbDolares.setText("Dólares");
        rbDolares.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbDolaresActionPerformed(evt);
            }
        });

        jLabel8.setText("Moneda");

        jLabel16.setText("Monto final dólares");

        jLabel13.setText("Monto final soles");
        jLabel13.setMaximumSize(new java.awt.Dimension(109, 14));
        jLabel13.setMinimumSize(new java.awt.Dimension(109, 14));
        jLabel13.setPreferredSize(new java.awt.Dimension(109, 14));

        jLabel9.setText("Tipo");

        rbBoleta.setText("Boleta");

        rbFactura.setText("Factura");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8)
                            .addComponent(jLabel9))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(rbSoles)
                                .addGap(18, 18, 18)
                                .addComponent(rbDolares))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(rbBoleta)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(rbFactura))))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel16)
                            .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 72, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtFinalDolares, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtFinalSoles, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(rbBoleta)
                    .addComponent(rbFactura))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(rbSoles)
                    .addComponent(rbDolares))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtFinalSoles, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtFinalDolares, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel16))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnBuscar.setText("Buscar");
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });

        btnLimpiar.setText("Limpiar");
        btnLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimpiarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnBuscar)
                        .addGap(18, 18, 18)
                        .addComponent(btnLimpiar))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnLimpiar)
                    .addComponent(btnBuscar))
                .addGap(19, 19, 19))
        );

        btnRegistrar.setText("Registrar");
        btnRegistrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRegistrarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnRegistrar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnRegistrar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(102, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
        if(valido()){
            long numeroPed = (txtNumPedido.getText().equals(""))?0:Long.parseLong(txtNumPedido.getText());
            Date fechaPed = (dpFechaPed.getDate()==null)?null:dpFechaPed.getDate();
                        
            long numeroCot = (txtNumCotizacion.getText().equals(""))?0:Long.parseLong(txtNumCotizacion.getText());
            Date fechaCot = (dpFechaCot.getDate()==null)?null:dpFechaCot.getDate();
            
            String nombre = (txtNombre.getText().equals(""))?null:"%"+txtNombre.getText()+"%";
            String numDoc = (txtNumDoc.getText().equals(""))?null:txtNumDoc.getText();
            
            Date fechaEmision = (dpFechaVenta.getDate()==null)?null:dpFechaVenta.getDate();
            
            String tipoDoc;
            if(rbBoleta.isSelected())
                tipoDoc = "Boleta";
            else if (rbFactura.isSelected())
                tipoDoc = "Factura";
            else
                tipoDoc=null;
            
            String tipoMoneda;
            if(rbSoles.isSelected())
                tipoMoneda = "Soles";
            else if (rbDolares.isSelected())
                tipoMoneda = "Dolares";
            else
                tipoMoneda=null;
            
            double totalSoles = (txtFinalSoles.getText().equals(""))?0:Double.parseDouble(txtFinalSoles.getText());
            double totalDolares = (txtFinalDolares.getText().equals(""))?0:Double.parseDouble(txtFinalDolares.getText());
            
            FacturacionBusqueda facturacionBusqueda = new FacturacionBusqueda(null,null,null,null,fechaEmision,totalSoles,null,tipoDoc,numeroPed,fechaPed,numeroCot,fechaCot,nombre,nombre,nombre,nombre,numDoc,0,tipoMoneda,0,totalDolares);
            ArrayList<FacturacionBusqueda> docsConsulta = GestionVentas.getInstance().queryDocs(facturacionBusqueda);
            refreshItemsTable2(docsConsulta);
    }   
    }//GEN-LAST:event_btnBuscarActionPerformed

    private void btnLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimpiarActionPerformed
        limpiar();
    }//GEN-LAST:event_btnLimpiarActionPerformed

    private void tblDocsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblDocsMouseClicked
        if(evt.getClickCount()==2){
            int rowSel = tblDocs.getSelectedRow();
            FacturacionVerJDialog verDialog = new FacturacionVerJDialog(null, true);
            verDialog.setDoc(documentos.get(rowSel));
            verDialog.setVisible(true);
        }
    }//GEN-LAST:event_tblDocsMouseClicked

    private void rbDolaresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbDolaresActionPerformed
        refreshDetails();
    }//GEN-LAST:event_rbDolaresActionPerformed

    private void btnRegistrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRegistrarActionPerformed
        FacturacionRegistrarJDialog registrarFacDialog = new FacturacionRegistrarJDialog(null, true);
        registrarFacDialog.setRefPanel(this);
        registrarFacDialog.setVisible(true);
    }//GEN-LAST:event_btnRegistrarActionPerformed

    private void rbSolesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbSolesActionPerformed
        refreshDetails();
    }//GEN-LAST:event_rbSolesActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscar;
    private javax.swing.ButtonGroup btnGpMoneda;
    private javax.swing.ButtonGroup btnGpTipo;
    private javax.swing.JButton btnLimpiar;
    private javax.swing.JButton btnRegistrar;
    private org.jdesktop.swingx.JXDatePicker dpFechaCot;
    private org.jdesktop.swingx.JXDatePicker dpFechaPed;
    private org.jdesktop.swingx.JXDatePicker dpFechaVenta;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JRadioButton rbBoleta;
    private javax.swing.JRadioButton rbDolares;
    private javax.swing.JRadioButton rbFactura;
    private javax.swing.JRadioButton rbSoles;
    private javax.swing.JTable tblDocs;
    private javax.swing.JTextField txtFinalDolares;
    private javax.swing.JTextField txtFinalSoles;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtNumCotizacion;
    private javax.swing.JTextField txtNumDoc;
    private javax.swing.JTextField txtNumPedido;
    // End of variables declaration//GEN-END:variables
}
